package com.kouxin.appaccount.software;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.kouxin.appaccount.developer.Developer;
import com.kouxin.appaccount.developer.DeveloperAuthenticationInterceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PricingStrategyAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 723312817004896525L;
	private Software software;
	private SoftwareDao softwareDao;
	private Map<String, Object> session;
	
	private List<PricingStrategy> pricingStrategys;
	private PricingStrategyDao pricingStrategyDao;
	
	/**
	 * 设置软件价格策略
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String appprice() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		HttpServletRequest request = ServletActionContext.getRequest();
		software = softwareDao.get(request.getParameter("software.id"));
		if (software == null || !developer.getId().equals(software.getDeveloperId())) {
			this.addActionError("找不到指定的软件");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return ERROR;
		} else {
			PricingStrategy pricingStrategy = new PricingStrategy();
			pricingStrategy.setSoftwareId(software.getId());
			this.pricingStrategys = pricingStrategyDao.findByExample(pricingStrategy);
			return INPUT;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String savePricingStrategy() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		HttpServletRequest request = ServletActionContext.getRequest();
		software = softwareDao.get(request.getParameter("software.id"));
		if (software == null || !developer.getId().equals(software.getDeveloperId())) {
			this.addActionError("找不到指定的软件");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return ERROR;
		} else {
			pricingStrategyDao.bind(software, this.pricingStrategys);
			return SUCCESS;
		}
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public Software getSoftware() {
		return software;
	}

	public void setSoftware(Software software) {
		this.software = software;
	}

	public SoftwareDao getSoftwareDao() {
		return softwareDao;
	}

	public void setSoftwareDao(SoftwareDao softwareDao) {
		this.softwareDao = softwareDao;
	}

	public List<PricingStrategy> getPricingStrategys() {
		return pricingStrategys;
	}

	public void setPricingStrategys(List<PricingStrategy> pricingStrategys) {
		this.pricingStrategys = pricingStrategys;
	}
	
	public PricingStrategyDao getPricingStrategyDao() {
		return pricingStrategyDao;
	}

	public void setPricingStrategyDao(PricingStrategyDao pricingStrategyDao) {
		this.pricingStrategyDao = pricingStrategyDao;
	}
}
